package com.sacc.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.power.common.model.CommonResult;
import com.sacc.mapper.AddrUserMapper;
import com.sacc.pojo.AddrUser;
import com.sacc.service.AddrUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 滚~韬
 * @date 2022/4/9 15:28
 */
@Service
public class AddrUserServiceImpl implements AddrUserService {
    @Autowired
    private AddrUserMapper addrUserMapper;

    /**
     * 更新用户地址
     * @param addrUser
     * @return
     */
    @Override
    public CommonResult Update(AddrUser addrUser) {
        try{
            //如果更新地址为默认,把之前的默认地址修改为不默认
            if(addrUser.getIsDefault()==1){
                AddrUser defaultAddr = addrUserMapper.selectOne(new QueryWrapper<AddrUser>()
                        .eq("open_id", addrUser.getOpenId())
                        .eq("is_default", 1));
                defaultAddr.setIsDefault(0);
                addrUserMapper.updateById(defaultAddr);
            }
            //如果更新地址不为默认,检测有没有默认,如果之前不是默认,直接更新,否则,那就默认第一个是默认
            else {
                AddrUser defaultAddr = addrUserMapper.selectOne(new QueryWrapper<AddrUser>()
                        .eq("open_id", addrUser.getOpenId())
                        .eq("is_default", 1));
                if(defaultAddr==null){
                    List<AddrUser> collect = addrUserMapper.selectList(new QueryWrapper<AddrUser>().eq("open_id",addrUser.getOpenId())).stream().limit(1).collect(Collectors.toList());
                    collect.forEach(c->{
                        c.setIsDefault(1);
                        addrUserMapper.updateById(c);
                    });
                }
            }
            addrUserMapper.updateById(addrUser);
            return new CommonResult("200","更新地址成功");
        }catch (Exception e){
            return new CommonResult("400","更新地址失败:"+e.toString());
        }
    }

    /**
     * 增加用户地址
     * @param addrUser
     * @return
     */
    @Override
    public CommonResult Add(AddrUser addrUser) {
        try{
            addrUser.setCreateTime(LocalDateTime.now());
            //如果添加的默认地址那么就把之前的默认地址设置为不默认
            if(addrUser.getIsDefault()==1){
                AddrUser defaultAddr = addrUserMapper.selectOne(new QueryWrapper<AddrUser>()
                        .eq("open_id", addrUser.getOpenId())
                        .eq("is_default", 1));
                if(defaultAddr!=null) {
                    defaultAddr.setIsDefault(0);
                    addrUserMapper.updateById(defaultAddr);
                }
            }
            addrUserMapper.insert(addrUser);
            return new CommonResult("200","增加地址成功");
        }catch (Exception e){
            return new CommonResult("400","增加地址失败:"+e.toString());
        }
    }

    /**
     * 得到用户地址
     * @param openId
     * @return
     */
    @Override
    public List<AddrUser> Get(String openId) {
            return addrUserMapper.selectList(new QueryWrapper<AddrUser>().eq("open_id",openId));
    }

    /**
     * 删除用户地址
     * @param id
     * @return
     */
    @Override
    public CommonResult Delete(String id) {
        try{
            AddrUser addrUser = addrUserMapper.selectById(id);
            //如果删除的是默认地址,把原来的默认地址修改为不是默认
            if(addrUser.getIsDefault()==1){
                List<AddrUser> collect = addrUserMapper.selectList(new QueryWrapper<AddrUser>().eq("open_id",addrUser.getOpenId())).stream().limit(1).collect(Collectors.toList());
                collect.forEach(c->{
                    c.setIsDefault(1);
                    addrUserMapper.updateById(c);
                });
            }
            addrUserMapper.deleteById(id);
            return new CommonResult("200","删除地址成功");
        }catch (Exception e){
            return new CommonResult("400","删除地址失败:"+e.toString());
        }
    }

    /**
     * 根据用户id获取默认地址
     * @param opneId
     * @return
     */
    @Override
    public AddrUser getDefault(String opneId) {
        AddrUser defaultAddr = addrUserMapper.selectOne(new QueryWrapper<AddrUser>()
                .eq("open_id", opneId)
                .eq("is_default", 1));
        return defaultAddr;
    }
}
